﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.DKChung
{
    public partial class DKChung_DanhSachCuaHang : UserControl
    {
        public DKChung_DanhSachCuaHang()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            gridCuaHang.DataSource = DataAll.SelectAll("DkChung_CuaHang", "*", "");
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            //gvNhomHang.BestFitColumns();
            gvCuaHang.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            //gvCuaHang.OptionsBehavior.Editable = false;
            gvCuaHang.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvCuaHang.OptionsSelection.MultiSelect = true;
            gvCuaHang.OptionsView.ShowGroupPanel = false;
            gvCuaHang.OptionsView.ColumnAutoWidth = true;
            gvCuaHang.OptionsView.ShowFooter = true;
            gvCuaHang.OptionsFind.AlwaysVisible = true;
            gvCuaHang.Columns["ID"].Visible = false;
        }

        private void bntThemMoi_Click(object sender, EventArgs e)
        {
            var mausac = new DKChung_DKCuaHang();
            mausac.ShowDialog();
            LoadData();
        }

        private void gvCuaHang_DoubleClick(object sender, EventArgs e)
        {
            if (gvCuaHang.FocusedRowHandle >= 0)
            {
                DKChung_DKCuaHang.MaID = gvCuaHang.Columns["ID"].View.GetRowCellDisplayText(gvCuaHang.FocusedRowHandle, "ID");
                var dkkho = new DKChung_DKCuaHang();
                dkkho.update = true;
                dkkho.ShowDialog();
                LoadData();
            }
        }

        private void btnXoa_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dataview = (DataView)gvCuaHang.DataSource;
             DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa cửa hàng này ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             if (dialogResult == DialogResult.Yes)
             {
                 string message = DataAll.Delete("DkChung_CuaHang", "ID = " + dataview[gvCuaHang.FocusedRowHandle]["ID"]);
                 MessageBox.Show(message.Equals("success") ? "Xóa thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
             }
            LoadData();
        }
    }
}
